<?php
/**
 * Created by PhpStorm.
 * User: eeo
 * Date: 2017/6/30
 * Time: 10:42
 */

namespace App\Http\Controllers\Foundation;
use App\Model\Foundation\StudentRemark;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;

class StudentRemarkController extends BaseController
{
    use MessagePacket;

    /**
     * 根据备注id获取单条备注
     * @param $id
     * @return packetMessage
     */
    public function get($id)
    {
        $id = intval($id);
        $obj = StudentRemark::find($id);
        if(empty($obj)){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }

    /**
     * 获取某个教师的备注列表
     * @param $id
     * @return packetMessage
     */
    function gets($id,Request $request)
    {
        $id = intval($id);
        $query =  StudentRemark::where('teacheruid', $id);
        if($request->has('classeid')){
            $query->where('classeid',$request->classeid);
        }
        $data = $query->get();
        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }

    /**
     * 获取某个学生的所有备注
     * @param $id
     * @return packetMessage
     */
    function getSingleStudentRemarks($id){
        $id = intval($id);
        $data =  StudentRemark::where('studentid', $id)->get();
        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }

    /**
     * 添加某班某学生的备注
     * @param Request $request
     * @return packetMessage
     */
    public function add(Request $request){
        $obj = new StudentRemark($request->all());
        if(!$obj->save()){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);

    }

    public function update($id,Request $request){

        $obj = StudentRemark::find($id);
        $obj->update($request->all());
        if(!$obj->save()){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }

    public function  delete($id){
        if(StudentRemark::destroy($id)){
            return $this->success(true);
        }
        return $this->fail(false,'删除失败');
    }
}